package com.ease.database.ddl.initializer;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.ease.common.util.DateFormatUtil;
import com.ease.common.vo.MapParameter;
import com.ease.dao.JdbcTemplateDataAccessObject;
import com.ease.platform.access.service.DatabaseColumnAccessService;
import com.ease.platform.access.service.DatabaseTableAccessService;
import com.obas.access.metadata.service.IndicatorTypeAccessService;
import com.obas.model.metadata.IndicatorType;
import com.obas.test.ApplicationContextUserContextTest;

public class AnalogyDateInitializer extends ApplicationContextUserContextTest {

	@Autowired
	JdbcTemplateDataAccessObject jdbcTemplateDataAccessObject;

	@Autowired
	DatabaseTableAccessService databaseTableAccessService;

	@Autowired
	DatabaseColumnAccessService databaseColumnAccessService;

	@Autowired
	IndicatorTypeAccessService indicatorTypeAccessService;

	@Test
	public void init() {

		List<IndicatorType> indicatorTypes = indicatorTypeAccessService.findAll();
		for (IndicatorType indicatorType : indicatorTypes) {
			Date date = DateFormatUtil.toDatetime("2013-06-01 13:00:00");
			int batchIndex = 24 * 7 * 52;
			while (batchIndex-- > 0) {
				init(indicatorType, date);
				date = DateUtils.addHours(date, 1);
			}
		}

	}

	public void init(IndicatorType indicatorType, Date batchTimestamp) {

		long[] customerIds = new long[] {//
		//
				4519401,//
				4519402,//
				4519403,//
				4519404,//
				4519405,//
				4519406,//
				4519407,//
				4519408,//
				4519409,//
				4519410 //
		};

		for (long customerId : customerIds) {
			init(indicatorType, batchTimestamp, customerId);
		}

	}

	public void init(IndicatorType indicatorType, Date batchTimestamp, long customerId) {
		int[] businessApplicationIdentitys = new int[] {//
		//
				724,//
				729,//
				725,//
				728,//
				727,//
				732,//
				721,//
				731,//
				722,//
				726 //
		};

		for (int businessApplicationIdentity : businessApplicationIdentitys) {
			init(indicatorType, batchTimestamp, customerId, businessApplicationIdentity);
		}
	}

	public void init(IndicatorType indicatorType, Date batchTimestamp, long customerId, int businessApplicationIdentity) {
		MapParameter parameter = new MapParameter();

		parameter.put("BATCHTIMESTAMP", batchTimestamp);
		parameter.put("INDICATORTYPE_ID", indicatorType.getId());
		parameter.put("NETWORKSERVING_ID", 1);
		parameter.put("VALUE1", random());
		parameter.put("VALUE2", random());
		parameter.put("VALUE3", random());
		parameter.put("VALUE4", random());
		parameter.put("VALUE5", random());
		parameter.put("VALUE6", random());
		parameter.put("VALUE7", random());
		parameter.put("VALUE8", random());
		parameter.put("VALUE9", random());
		parameter.put("VALUE10", random());
		parameter.put("VALUE11", random());
		parameter.put("VALUE12", random());
		parameter.put("SUMMATION", 0);
		parameter.put("CUSTOMER_ID", customerId);
		parameter.put("BUSINESSAPPLICATION_IDENTITY", businessApplicationIdentity);

		jdbcTemplateDataAccessObject
				.create("INSERT INTO X_USERAPP_HOUR_KPI_AGG(BATCHTIMESTAMP,INDICATORTYPE_ID,NETWORKSERVING_ID,VALUE1,VALUE2,VALUE3,VALUE4,VALUE5,VALUE6,VALUE7,VALUE8,VALUE9,VALUE10,VALUE11,VALUE12,SUMMATION,CUSTOMER_ID,BUSINESSAPPLICATION_IDENTITY) VALUES(:BATCHTIMESTAMP,:INDICATORTYPE_ID,:NETWORKSERVING_ID,:VALUE1,:VALUE2,:VALUE3,:VALUE4,:VALUE5,:VALUE6,:VALUE7,:VALUE8,:VALUE9,:VALUE10,:VALUE11,:VALUE12,:SUMMATION,:CUSTOMER_ID,:BUSINESSAPPLICATION_IDENTITY)",
						parameter);
	}

	static int random() {
		return RandomUtils.nextInt(1000);
	}

}
